import React, { Component } from "react";
import {
  Link,
  Route,
  Switch,
  useHistory,
  useLocation,
  useParams,
  useRouteMatch,
} from "react-router-dom";

// hooks,钩子
// hooks是16.8版本新增的，只能给函数组件用
// 增强函数组件的能力

// 在使用高阶组件的时候，会使得组件树变得更深
// hooks都是函数
// hooks必须在函数组件的顶层调用

class Home extends Component {
  render() {
    return <div>这是home组件</div>;
  }
}

class About extends Component {
  render() {
    return <div>这是About组件</div>;
  }
}

// const Mine = withRouter((props) => {
//   console.log(props);
//   return <div>这是我的组件</div>;
// });

const Mine = () => {
  const history = useHistory();
  const location = useLocation();
  const params = useParams();
  const match = useRouteMatch();

  console.log(location);
  console.log(params);
  console.log(match);
  // console.log(history);

  const fn = () => {
    history.push("/home");
  };
  // const history = useHistory();
  return (
    <div>
      这是我的组件 - <button onClick={fn}>btn</button>
    </div>
  );
};

class App extends Component {
  state = {};
  render() {
    return (
      <>
        <h2>react-router-hooks</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于</Link>
          </li>
          <li>
            <Link to="/mine">我的</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine">
            <Mine></Mine>
          </Route>
        </Switch>
      </>
    );
  }
}

export default App;
